﻿using System.Data;
using System.Security.Cryptography.X509Certificates;
using System.ComponentModel.Design;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.OrgManage;
using AssetManageSystem.Model;
using AssetManageSystem.ViewModel.AssetManage.AssetRepairVMs;
using AssetManageSystem.ViewModel.AssetManage.AssetAllocationVMs;
using AssetManageSystem.ViewModel.AssetManage.AssetBorrowVMs;


namespace AssetManageSystem.ViewModel.AssetManage.AssetInfoVMs
{
    public partial class AssetInfoVM : BaseCRUDVM<AssetInfo>
    {
        public List<ComboSelectListItem> AllTypes { get; set; }
        public List<ComboSelectListItem> AllUsePeoples { get; set; }
        public List<ComboSelectListItem> AllUseDeps { get; set; }
        public AssetAllocationListVM AssetAllocationList { get; set; }
        public AssetRepairListVM AssetRepairList { get; set; }
        public AssetBorrowListVM AssetBorrowList { get; set; }

        public AssetInfoVM()
        {
            SetInclude(x => x.Type);
            SetInclude(x => x.UsePeople);
            SetInclude(x => x.UseDep);
        }

        protected override void InitVM()
        {
            //资产调拨记录
            AssetAllocationList = new AssetAllocationListVM();
            AssetAllocationList.CopyContext(this);
            AssetAllocationList.SearcherMode = ListVMSearchModeEnum.MasterDetail;
            AssetAllocationList.Searcher.AssetId = Entity.ID;

            //资产维修记录
            AssetRepairList = new AssetRepairListVM();
            AssetRepairList.CopyContext(this);
            AssetRepairList.SearcherMode = ListVMSearchModeEnum.MasterDetail;
            AssetRepairList.Searcher.RepairAssetId = Entity.ID;

            //资产借用记录
            AssetBorrowList = new AssetBorrowListVM();
            AssetBorrowList.CopyContext(this);
            AssetBorrowList.SearcherMode = ListVMSearchModeEnum.MasterDetail;
            AssetBorrowList.Searcher.AssetId = Entity.ID;

            AllTypes = DC.Set<AssetType>().Where(x => x.ParentId != null).GetSelectListItems(Wtm, y => y.TypeName);
            AllUsePeoples = DC.Set<User>().GetSelectListItems(Wtm, y => y.UserName);
            AllUseDeps = DC.Set<Department>().GetSelectListItems(Wtm, y => y.DepName);
            Entity.AsCode = "AS" + Helper.RandomNumber.GetRandom();
        }

        public override void DoAdd()
        {
            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }

        /// <summary>
        /// 根据资产状态统计资产数量
        /// </summary>
        /// <returns>Dictionary</returns>
        public Dictionary<string, int> GetAssetCountByStatus()
        {
            var result = from d in DC.Set<AssetInfo>()
                group d by d.Status into g select (new { g.Key, Count = g.Count() });
            var data = result.ToDictionary(x => x.Key.ToString(), x => x.Count);
            if (!data.ContainsKey("OnUse"))
            {
                data.Add("OnUse", 0);
            }
            if (!data.ContainsKey("Repair"))
            {
                data.Add("Repair", 0);
            }
            if (!data.ContainsKey("NoUse"))
            {
                data.Add("NoUse", 0);
            }
            if (!data.ContainsKey("Scrap"))
            {
                data.Add("Scrap", 0);
            }
            if (!data.ContainsKey("Donation"))
            {
                data.Add("Donation", 0);
            }
            if (!data.ContainsKey("Borrow"))
            {
                data.Add("Borrow", 0);
            }

            return new Dictionary<string, int>()
            {
                {"OnUse",data["OnUse"]},
                {"Repair",data["Repair"]},
                {"NoUse",data["NoUse"]},
                {"Scrap",data["Scrap"]},
                {"Donation",data["Donation"]},
                {"Borrow",data["Borrow"]}
            };
        }


        /// <summary>
        /// 根据资产类型统计资产数量
        /// </summary>
        /// <returns>Dictionary</returns>
        public Dictionary<string, int> GetAssetCountByType()
        {
            var result = from d in DC.Set<AssetInfo>()
                         where (d.Status == Model.Enum.AsStatusEnum.OnUse)
                         group d by d.Type.TypeName into g
                         select new { g.Key, Count = g.Count() };
            var data = result.ToDictionary(x => x.Key, x => x.Count);
            if (!data.ContainsKey("台式计算机"))
            {
                data.Add("台式电脑", 0);
            }
            if (!data.ContainsKey("笔记本电脑"))
            {
                data.Add("笔记本电脑", 0);
            }
            if (!data.ContainsKey("打印机"))
            {
                data.Add("打印机", 0);
            }
            if (!data.ContainsKey("投影仪"))
            {
                data.Add("投影仪", 0);
            }
            if (!data.ContainsKey("扫描仪"))
            {
                data.Add("扫描仪", 0);
            }
            if (!data.ContainsKey("显示器"))
            {
                data.Add("显示器", 0);
            }
            if (!data.ContainsKey("路由器"))
            {
                data.Add("路由器", 0);
            }
            if (!data.ContainsKey("交换机"))
            {
                data.Add("交换机", 0);
            }
            if (!data.ContainsKey("服务器"))
            {
                data.Add("服务器", 0);
            }

            data.Add("电脑", data["台式电脑"] + data["笔记本电脑"]);
            //data.Add("软件", DC.Set<Software>().Count());

            return new Dictionary<string, int>(){
                {"computer",data["电脑"]},
                //{"software",data["软件"]},
                {"printer",data["打印机"]},
                {"scanner",data["扫描仪"]},
                {"projector",data["投影仪"]},
                {"monitor",data["显示器"]},
                {"router",data["路由器"]},
                {"switcher",data["交换机"]},
                {"server",data["服务器"]}
            };
        }

    }
}
